﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.IO;

namespace imaginecup.Web
{
    /// <summary>
    /// 上传照片模块
    /// </summary>
    //[WebService(Namespace = "http://tempuri.org/")]
    //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class UpLoadImage : IHttpHandler
    {
        private string filepath;

        public void ProcessRequest(HttpContext context)
        {
            
            Stream sr = context.Request.InputStream;
            int byteRead = 0;
            byte[] buffer = new byte[4096];
            string filename = context.Request["filename"];

            filepath = HttpContext.Current.Server.MapPath("/photo/" + filename);

            try
            {
                using (FileStream fs = File.Create(filepath, 4096))
                {
                    while((byteRead = sr.Read(buffer,0,buffer.Length)) > 0)
                    {
                        fs.Write(buffer, 0, byteRead);
                    }
                }
            }
            catch(Exception e)
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write("UpLoad Error");
            }
            finally
            {
                sr.Dispose();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
